﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Reflection;
using System.ComponentModel;
using System.Windows.Data;

namespace WpfBrushes
{
    class MainViewModel 
    {
        private readonly BrushInfo[] brushInfos;
        private string search;

        public MainViewModel()
        {
            var brushes = typeof(Brushes).GetProperties(BindingFlags.Static | BindingFlags.Public);
            var query =
                from propertyInfo in brushes
                let brush = propertyInfo.GetValue(null, null) as SolidColorBrush
                where brush != null
                orderby propertyInfo.Name
                select new BrushInfo(propertyInfo.Name, brush);


            brushInfos = query.ToArray();
            Items = CollectionViewSource.GetDefaultView(brushInfos);

            search = string.Empty;
            Items.Filter = obj =>
                {
                    var brushInfo = (BrushInfo)obj;
                    return brushInfo.Name.ToLower().Contains(search.ToLower());
                };
        }

        public ICollectionView Items { get; private set; }

        public string Search
        {
            get { return search; }
            set 
            { 
                search = value;
                Items.Refresh();
            }
        }
    }
}
